package com.miage.kangourou.server.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.miage.kangourou.client.beans.Groupe;

public class GroupeDao extends GenericDao<Groupe> {

	public GroupeDao(Connection connect) {
		super(connect);
	}

	public boolean alreadyExist(Groupe groupe) throws SQLException {
		boolean exist = false;

		if ((groupe instanceof Groupe) && !groupe.equals(null)) {

			ResultSet result = connect.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY).executeQuery(
					"SELECT * FROM GROUPE WHERE ID_GROUPE  = "
							+ groupe.getId_groupe());

			if (result.next()) {
				exist = true;
			}

			result.close();
		}

		return exist;
	}

	public void create(Groupe groupe) throws SQLException {
		if ((groupe instanceof Groupe) && !groupe.equals(null)) {

			connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY).executeQuery(
					"INSERT INTO GROUPE (ID_GROUPE, ID_PROMO, INTITULE , COULEUR) VALUES ("
							+ groupe.getId_groupe() + ", "
							+ groupe.getId_promo() + ", \""
							+ groupe.getIntitule() + ", \"" + groupe.getColor()
							+ "\")");
		}
	}

	public boolean delete(int idGroupe) {
		boolean deleted = true;

		try {
			connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY).executeQuery(
					"DELETE FROM GROUPE WHERE ID_GROUPE = " + idGroupe);

		} catch (SQLException e) {
			e.printStackTrace();
			deleted = false;
		}

		return deleted;
	}

	public List<Groupe> findAll() throws SQLException {
		ResultSet result = connect.createStatement(
				ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)
				.executeQuery("SELECT * FROM GROUPE");

		List<Groupe> all = new ArrayList<Groupe>();
		while (result.next()) {
			all.add(new Groupe(result.getInt("ID_GROUPE"), result
					.getInt("ID_PROMO"), result.getString("INTITULE"), result
					.getString("COULEUR")));
		}

		result.close();

		return all;
	}

	public Groupe findById(int id) throws SQLException {
		ResultSet result = connect.createStatement(
				ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)
				.executeQuery("SELECT * FROM GROUPE WHERE ID_GROUPE = " + id);

		Groupe groupe;

		if (result.next()) {
			groupe = new Groupe(result.getInt("ID_GROUPE"), result
					.getInt("ID_PROMO"), result.getString("INTITULE"), result
					.getString("COULEUR"));
		} else {
			groupe = new Groupe();
		}

		result.close();

		return groupe;
	}

	public List<Groupe> findByIdPromo(int promo) throws SQLException {
		ResultSet result = connect.createStatement(
				ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)
				.executeQuery("SELECT * FROM GROUPE WHERE ID_PROMO = " + promo);

		List<Groupe> all = new ArrayList<Groupe>();
		while (result.next()) {
			all.add(new Groupe(result.getInt("ID_GROUPE"), result
					.getInt("ID_PROMO"), result.getString("INTITULE"), result
					.getString("COULEUR")));
		}

		result.close();

		return all;
	}
}
